package generators;

import gamebackend.Block;

public class DoorGenerator extends AbstractGenerator implements BlockGenerator{
	private final int my_rowsBetween;
	private int my_rowsBeforeNext;
	public DoorGenerator(final Block the_block, final int the_rowsBetween) {
		super(the_block);
		my_rowsBetween = the_rowsBetween;
		my_rowsBeforeNext = my_rowsBetween;
	}
	public void generate(Block[][] blocks) {
		my_rowsBeforeNext--;
		if (my_rowsBeforeNext < 0) {
			my_rowsBeforeNext = my_rowsBetween;
			
			// make a door at the row with the fewest empty spots
			int minIndex = 0;
			int minValue = blocks[0].length;
			
			int stop = Math.min(my_rowsBetween, blocks.length);
			for (int row = 0; row < stop; row++) {
				int empties = 0;
				for (int col = 0; col < blocks[row].length; col++) {
					if (!blocks[row][col].isOccupied()) {
						empties++;
					}
				}
				if (minValue > empties) {
					minIndex = row;
					minValue = empties;
				}
			}
			for (int col = 0; col < blocks[0].length; col++) {
				if (!blocks[minIndex][col].isPath()) {
					blocks[minIndex][col] = getBlock();
				}
			}
		}
	}
}
